// pages/mystudent/index.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    user: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
   
  },
  onShow: function(options) {
    if (!app.globalData.userInfo) {
      wx.showModal({
        title: '提示',
        content: '你还没登录，请登录后再报名！',
        success(res) {
          if (res.confirm) {
            app.check();
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        }
      })
      return;
    }
    //获取学员
    wx.request({
      url: 'https://sng.tsletu.com/api/wxapp/user/getUser',
      header: {
        'XX-Device-Type': 'wxapp',
        'XX-Token': app.globalData.token
      },
      success: res => {
        this.setData({
          user: res.data.data
        })
      }
    })
  },
  getPhoneNumber(e) {    
    wx.login({
      success: (res) => {
        wx.request({
          url: 'https://sng.tsletu.com/api/wxapp/user/bindphone',
          method: 'get',
          data: {
            code: res.code,
            iv: e.detail.iv,
            encrypted_data: e.detail.encryptedData
          },
          header: {
            'XX-Device-Type': 'wxapp',
            'XX-Token': app.globalData.token
          },
          success: (res) => {            
            app.globalData.userInfo.mobile = res.data.data.phoneNumber;
            this.onShow()            
          }
        })
      }
    })
    
  } 
  
})